package org.ysh.service;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.ysh.core.cache.Constants;
import org.ysh.domain.Fonds;
import org.ysh.domain.User;
import org.ysh.mapper.BaseMapper;
import org.ysh.mapper.FondsMapper;
import org.ysh.vo.FondsTreeNodeVo;
import org.ysh.vo.FondsVo;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FondsService extends BaseService<Integer, Fonds>{

    public FondsService(FondsMapper baseMapper) {
        super(baseMapper);
    }

    public List<FondsTreeNodeVo> getFondsTree() {
        List<Fonds> fondsList = this.findByPage(new Fonds());
        return FondsTreeNodeVo.buildTree(fondsList);
    }

    public List<FondsVo> getFondsVoList() {
        return findByPage(new Fonds()).stream().map(FondsVo::new).collect(Collectors.toList());
    }

    public void deleteByFondsCode(String fondsCode) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER);
        findByPage(new Fonds(fondsCode)).forEach(fonds -> {
            Fonds newFonds = new Fonds();
            newFonds.setId(fonds.getId());
            newFonds.setDeleteBy(user.getId());
            this.delete(newFonds);
        });
    }

}
